
#include "OneTimeLinearParticleEmitter.h"
#include "../../Utils/Util.h"

OneTimeLinearParticleEmitter::OneTimeLinearParticleEmitter(unsigned num_particles_in_burst,
                                               Vector2D emitter_start_pos, 
                                               Vector2D emitter_end_pos, 
                                               Vector2D emit_velocity,
                                               float emit_rand_min, float emit_rand_max,

                                               float col_r, float col_g, float col_b,
                                               float life, float life_rand, 
                                               float size, float size_rand, 
                                               float life_decay, float life_decay_rand, 
                                               float size_decay, float size_decay_rand,
                                               std::string texture, bool flare_up) :
    
    ParticleEmitter(emitter_start_pos, emit_velocity, emit_rand_min, emit_rand_max, 0,
                    col_r, col_g, col_b, life, life_rand, size, size_rand, life_decay, 
                    life_decay_rand, size_decay, size_decay_rand, texture, flare_up),

    num_particles_in_burst(num_particles_in_burst), 
    done_burst(false),
    emitter_start_pos(emitter_start_pos), 
    emitter_end_pos(emitter_end_pos) {
    
}

OneTimeLinearParticleEmitter::~OneTimeLinearParticleEmitter(){

}

std::vector<Particle> OneTimeLinearParticleEmitter::emit(void){ 
    std::vector<Particle> result; 
    if(done_burst){ return result; }

    for(unsigned i = 0; i < num_particles_in_burst; i++){
        float np_life       = particle_life       + Util::rand_interval(-particle_life_rand, particle_life_rand);
        float np_size       = particle_size       + Util::rand_interval(-particle_size_rand, particle_size_rand);
        float np_life_decay = particle_life_decay + Util::rand_interval(-particle_life_decay_rand, particle_life_decay_rand);
        float np_size_decay = particle_size_decay + Util::rand_interval(-particle_size_decay_rand, particle_size_decay_rand);

        Vector2D perp = emitter_start_pos - emitter_end_pos;
        perp.normalise();
        perp.rotate(90.0f);

        Vector2D np_velocity = perp;
        np_velocity.scale(Util::rand_interval(-emit_rand_max, emit_rand_max));

        
        float pos_interval = Util::rand_interval(0.0f, 1.0f);
        np_velocity.scale(1.0f - pos_interval);
        Vector2D np_pos = emitter_start_pos + pos_interval*(emitter_end_pos - emitter_start_pos);

        Particle new_particle(particle_col_r, particle_col_g, particle_col_b,
                              np_life, np_size, np_life_decay, np_size_decay,
                              np_pos, np_velocity, use_texturing, texture_id, flare_up);

        result.push_back(new_particle);
    }

    done_burst = true;
    return result;
}

